package com.xuecheng.content.controller;


import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVo;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;

    @Override
    @GetMapping("/course/all")
    public List<CourseBase> queryAll() {
        return courseBaseService.queryAll();
    }

    @Override
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params, QueryCourseModel model) {
        //添加令牌  id
        Long companyId = SecurityUtil.getCompanyId();
        return this.courseBaseService.queryCourseList(params, model, companyId);
    }

    @Override
    @PostMapping("course")
    public CourseBaseDTO createCourse(@RequestBody CourseBaseVo courseBaseVo) {
        //添加令牌  id
        Long companyId = SecurityUtil.getCompanyId();
        //vo转换成dto传入service层
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVo);
        //设置机构id
        courseBaseDTO.setCompanyId(companyId);
        return this.courseBaseService.createCourse(courseBaseDTO);
    }

    @Override
    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseBaseById(@PathVariable Long courseBaseId) {
        //添加令牌  id
        Long companyId = SecurityUtil.getCompanyId();
        return this.courseBaseService.getCourseBaseById(courseBaseId, companyId);
    }

    @Override
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVo courseBaseVo) {
        //添加令牌  id
        Long companyId = SecurityUtil.getCompanyId();
        //vo转换成dto传入service层
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVo);
        //设置机构id
        courseBaseDTO.setCompanyId(companyId);
        return this.courseBaseService.modifyCourseBase(courseBaseDTO);
    }


    @Override
    @DeleteMapping("course/{coursebaseId}")
    public void removeCourseBase(@PathVariable Long coursebaseId) {
        //添加令牌  id
        Long companyId = SecurityUtil.getCompanyId();
        this.courseBaseService.removeCourseBase(coursebaseId, companyId);
    }

}
